package com.itheima.demo.controller;

import com.itheima.demo.dto.AddDeptDto;
import com.itheima.demo.dto.UpdateDeptDtp;
import com.itheima.demo.entity.Dept;
import com.itheima.demo.service.DeptService;
import com.itheima.demo.vo.DeptV0;
import com.itheima.demo.vo.Result2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //1. 接收参数
    @GetMapping
    public Result2<List<DeptV0>> depts(){
        //2. 调用service完成业务逻辑处理
        List<DeptV0> depts = deptService.depts();
        //3. 返回响应数据
        return Result2.ok(depts);
    }
    @DeleteMapping
/*    public Result2<Void> deleteById(HttpServletRequest request){
        String id = request.getParameter("id");
        deptService.deleById(Integer.parseInt(id));
        return Result2.ok();*/
    public Result2<Void> deleteById(@RequestParam("id")Integer id ){
                deptService.deleById(id);
                return Result2.ok();
        }


    /**
     *@PathVariable
     * 路径标量 路径参数
     */

     @GetMapping("/{id}")
    public  Result2<Dept> getByID(@PathVariable("id")Integer id){
       Dept dept= deptService.getById(id);
       return Result2.ok(dept);
     }
     @PutMapping
    public  Result2 updateDept(@RequestBody UpdateDeptDtp dto){
         deptService.updateDept(dto);
         return  Result2.ok();
     }
    @PostMapping
    public Result2 addDept(@RequestBody AddDeptDto dto){
        deptService.addDept(dto);
        return Result2.ok();
    }

}

